﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAO
{
    public static class DataProvider
    {
        private static SqlConnection _con = null;

        static DataProvider()
        {
            string strConnection = "Data Source=.\\SQLEXPRESS;Initial Catalog=ChoDienTu;Integrated Security=True";
            _con = new SqlConnection(strConnection);
        }

        // thực thi câu truy vấn sql
        public static DataTable ExecuteQuery(string strQuery)
        {
            DataTable resTable = new DataTable();
            try
            {
                _con.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(strQuery, _con);
                adapter.Fill(resTable);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thực thi lệnh SQL: " + ex.Message);
            }
            finally
            {
                _con.Close();
            }
            return resTable;
        }

        // thực thi câu truy vấn bằng StoreProcedure cần có: tên store procedure, danh sách tham số đầu vào
        public static DataTable ExecuteStoreProc(string storeProcName, IList<SqlParameter> arrParameter)
        {

            DataTable resTable = new DataTable();
            try
            {
                _con.Open();
                SqlCommand cmd = new SqlCommand(storeProcName, _con);
                cmd.CommandText = storeProcName;
                cmd.CommandType = CommandType.StoredProcedure;
                //Lấy số parameter
                int N = arrParameter.Count;
                for (int i = 0; i < N; i++)
                {
                    cmd.Parameters.Add(arrParameter[i]);
                }
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(resTable);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thực thi stored procedure: " + ex.Message);
            }
            finally
            {
                _con.Close();
            }
            return resTable;
        }

        //thực thi stored ko có tham số
        public static DataTable ExecuteStoreProcNoParamater(string storeProcName)
        {
            DataTable resTable = new DataTable();
            try
            {
                _con.Open();
                SqlCommand cmd = new SqlCommand(storeProcName, _con);
                cmd.CommandText = storeProcName;
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(resTable);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thực thi stored procedure: " + ex.Message);
            }
            finally
            {
                _con.Close();
            }
            return resTable;
        }
    }
}
